package com.neusoft.bizcore.activiti.components;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Component;

import com.neusoft.bizcore.auth.common.bean.UserBean;
import com.neusoft.bizcore.web.support.Searchable;

@Component
public class ActUserAuthorityConditionComponent {

    public String searchUserAuthRolesKey = "authRoles";

    public Searchable addUserRolesToSearchCondition(final Searchable searchable,
            final UserBean user) {

        if ((searchable.getBooleanValue(this.searchUserAuthRolesKey) != null)
                && searchable.getBooleanValue(this.searchUserAuthRolesKey)) {
            searchable.put("userName", user.getUsername());
            final List<String> roleList = new ArrayList<>();
            user.getRoles().stream().map(k -> k.getRole()).forEach(roleList::add);
            searchable.put("roles", roleList.toArray(new String[roleList.size()]));
        }

        return searchable;
    }

}
